package com.yaodian100.mall.activities;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.crm.activities.entity.Mgm;
import com.yaodian100.crm.activities.entity.MgmCoupon;
import com.yaodian100.crm.activities.service.MgmService;
import com.yaodian100.ec.coupon.service.CouponService;
import com.yaodian100.mall.member.util.UserData;

public class MgmAction extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private MgmService mgmService;
	@Autowired
	private CouponService couponService;

	private String[] coupon;
	private String[] userToEmail;
	private String[] userName;

	private String result;
	private List<MgmCoupon> list;

	public String addSendMgmInfo() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		List<Mgm> mgmInfolist = new ArrayList<Mgm>();
		UserData userData = new UserData(request, response);
		String memberId = userData.getMemberId();

		if (coupon != null && userToEmail != null && userName != null) {
			int mark = 0;
			for (int j = 0; j < coupon.length; j++) {
				System.out.println(mark);
				System.out.println(userToEmail[j]);
				if ("".equals(userToEmail[j])) {
					mark++;
				}
			}
			for (int j = 0; j < userName.length; j++) {
				System.out.println(mark);
				System.out.println(userName[j]);
				if ("".equals(userName[j])) {
					mark++;
				}
			}
			if (mark > 0) {

				list = new ArrayList<MgmCoupon>();
				list = mgmService.findMgmCoupon(memberId);
				result = "error";
				return "tomgm";
			}

			if (coupon != null && userToEmail != null && userName != null && mark == 0) {
				for (int i = 0; i < coupon.length; i++) {
					mgmInfolist.add(mgmService.addMgmInfo(memberId, coupon[i], userToEmail[i], userName[i]));
				}
				result = "right";
				return "tomgm";
			}
		}
		list = new ArrayList<MgmCoupon>();
		list = mgmService.findMgmCoupon(memberId);
		result = "error";
		return "tomgm";
	}

	/**
	 * 跳转首页
	 * 
	 * @return
	 */
	public String indexMgm() {
		return "index";
	}

	// public String addMgmCouponInfo() {
	// HttpServletRequest request = (HttpServletRequest)
	// ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	// HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
	// ServletActionContext.HTTP_RESPONSE);
	// System.out.println("===============华丽的分割线=================");
	// UserData userData = new UserData(request, response);
	// String memberId = userData.getMemberId();
	// @SuppressWarnings("unused")
	// String loginName = userData.getLoginId();
	// List<Mgm> mgmInfolist = new ArrayList<Mgm>();
	// if (coupon != null && userToEmail != null) {
	// int mark = 0;
	// for (int j = 0; j < coupon.length; j++) {
	// System.out.println(mark);
	// if (" ".equals(userToEmail[j])) {
	// mark++;
	// }
	//
	// }
	// if (mark > 1) {
	// list = new ArrayList<MgmCoupon>();
	// list = mgmService.findMgmCoupon(memberId);
	// result = "error";
	// return "tomgm";
	// // } else if (mark == 0) {
	// // list = new ArrayList<MgmCoupon>();
	// // list = mgmService.findMgmCoupon(memberId);
	// // result = "error0";
	// // return "tomgm";
	// }
	//
	// }
	// if (coupon != null && userToEmail != null) {
	// for (int i = 0; i < coupon.length; i++) {
	// if (" ".equals(userToEmail[i])) {
	// // 充值优惠券
	// // CouponItem couponItem = couponService.getCouponItemByCode(coupon[i]);
	// // couponService.regCouponItem(couponItem, memberId);
	// } else {
	// mgmInfolist.add(mgmService.addMgmInfo(memberId, coupon[i], userToEmail[i]));
	// }
	// }
	// result = "right";
	// }
	// System.out.println(mgmInfolist.size());
	// return "tomgm";
	// }

	/**
	 * 跳转到活动页
	 * 
	 * @return
	 */
	public String toMgm() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		String memberId = userData.getMemberId();
		list = mgmService.findMgmCoupon(memberId);
		result = "first";
		return "tomgm";
	}

	/**
	 * 通过连接/赠送完-----> （注册）活动 商品展示页
	 * 
	 * @return
	 */
	public String toShowGoods() {

		return "mgmGoods";
	}

	public String getMgm() {

		return "getmgm";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<MgmCoupon> getList() {
		return list;
	}

	public void setList(List<MgmCoupon> list) {
		this.list = list;
	}

	public String[] getCoupon() {
		return coupon;
	}

	public void setCoupon(String[] coupon) {
		this.coupon = coupon;
	}

	public String[] getUserToEmail() {
		return userToEmail;
	}

	public void setUserToEmail(String[] userToEmail) {
		this.userToEmail = userToEmail;
	}

	// public String[] getType() {
	// return type;
	// }
	//
	// public void setType(String[] type) {
	// this.type = type;
	// }

	@Override
	public void prepare() throws Exception {

	}

	public String[] getUserName() {
		return userName;
	}

	public void setUserName(String[] userName) {
		this.userName = userName;
	}

}
